//
//  CSYUserModel.swift
//  CSSinaMicroBlog_Test
//
//  Created by 姚彦兆 on 15/11/11.
//  Copyright © 2015年 姚彦兆. All rights reserved.
//

import UIKit

class CSYUserModel: NSObject,NSCoding {

    //            >>>>>>>SHOWRESULT>>>>>>>>{
    //                "access_token" = "2.00fu7oCG0tFG7h324e19ff98oUkqTD";
    //                "expires_in" = 157679999;
    //                "remind_in" = 157679999;
    //                uid = 5538369521;
    //            }
    
    var access_token : String?
    
    var expires_in : NSTimeInterval = 0 {
    
        didSet{
        
            expires_date = NSDate(timeIntervalSinceNow: expires_in)
            
        }
        
    }
    
    var expires_date : NSDate?
    
    var uid : String?
    
    var avatar_hd : String?
    
    var name : String?
    
    init(userInfo dict : [String : AnyObject]) {
        
        super.init()
        
        setValuesForKeysWithDictionary(dict)
        
    }

    override func setValue(value: AnyObject?, forUndefinedKey key: String) {}
    
    override var description : String {
    
        let keys = ["access_token" , "expires_in" , "expires_date" , "uid" , "avatar_hd" , "name"]
        
        return dictionaryWithValuesForKeys(keys).description
        
    }
    
    //进行用户信息的存储操作
    
    //对外提供用户信息的读取方法
    class func readUserPlist() -> CSYUserModel?{
     
        let path : String = (NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true).last! as NSString).stringByAppendingPathComponent("account.plist")
        
        if let account = NSKeyedUnarchiver.unarchiveObjectWithFile(path) as? CSYUserModel {
  
            return account
        
        }
        
        return nil
        
    }
    
    func saveUserAccount(){
    
        let path : String = (NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true).last! as NSString).stringByAppendingPathComponent("account.plist")
        
        NSKeyedArchiver.archiveRootObject(self, toFile: path)
    
    }
    
    //归档操作
    func encodeWithCoder(aCoder: NSCoder) {
        
        aCoder.encodeObject(access_token, forKey: "access_token")

        aCoder.encodeDouble(expires_in, forKey: "expires_in")
        
        aCoder.encodeObject(expires_date, forKey: "expires_date")
        
        aCoder.encodeObject(uid, forKey: "uid")
        
        aCoder.encodeObject(avatar_hd, forKey: "avatar_hd")
        
        aCoder.encodeObject(name, forKey: "name")
        
    }
    
    //解档操作
    required init?(coder aDecoder: NSCoder) {
        
        access_token = aDecoder.decodeObjectForKey("access_token") as? String
        
        expires_in = aDecoder.decodeDoubleForKey("expires_in")
        
        expires_date = aDecoder.decodeObjectForKey("expires_date") as? NSDate
        
        uid = aDecoder.decodeObjectForKey("uid") as? String
        
        avatar_hd = aDecoder.decodeObjectForKey("avatar_hd") as? String
        
        name = aDecoder.decodeObjectForKey("name") as? String
        
    }
    
    
    
    
    
    
}
